package com.dean.fileshare;

import com.dean.service.CsvOptService;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;

public class DeanFileShareService {

    private static final Logger logger = LoggerFactory.getLogger(DeanFileShareService.class);

    // 静态内部类实现单例模式
    // 懒汉式 jvm 保证线程安全  jvm 保证每个类只加载一次 内部类只有在调用的时候才加载
    private DeanFileShareService(){}

    private static class DeanFileShareServiceHolder{
        private final static DeanFileShareService INSTANCE = new DeanFileShareService();
    }

    public static DeanFileShareService getInstance(){
        return DeanFileShareService.DeanFileShareServiceHolder.INSTANCE;
    }

    public void downShareFile(ShareFileEntity shareFileEntity){

    }

    public void downShareFile(String src, String tar){
        SmbFile smbFile = null;
        SmbFileInputStream input = null;
        FileOutputStream output = null;
        try {
            smbFile = new SmbFile(src);
            input = new SmbFileInputStream(smbFile);
            output = new FileOutputStream(tar);
            byte[] buff = new byte[1024];
            int read;
            while ((read = input.read(buff))>0){
                output.write(buff,0,read);
            }
            output.flush();

        } catch (SmbException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }  catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(output!=null){
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            };
            if(input!=null){
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            };
        }


    }

}
